package com.TaskOrganizer.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.TaskOrganizer.core.AppCore;
import com.TaskOrganizer.task.Task;

public class TaskDialog extends JFrame {

	private static final long serialVersionUID = -6644541447195281453L;
	private Task task;
	private JLabel descriptionLabel, nameLabel, priorityLabel,
			dateCreatedLabel;
	private JTextArea descriptionTextArea, nameTextArea, priorityTextArea,
			dateCreatedTextArea;
	private JButton delete, cancel, update, resolve;
	private JScrollPane descriptionScrollPane, nameScrollPane;
	private final boolean editable = false;

	public TaskDialog(Task task) {
		super(task.getName());
		this.task = task;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
		Toolkit kit = Toolkit.getDefaultToolkit();
		URL url = getClass().getResource("/com/TaskOrganizer/icons/icon.png");
		Image img = kit.createImage(url);
		setIconImage(img);
		setLayout(new GridLayout(6, 2));

		getNamePanel();
		getDateCreatedPanel();
		getDescriptionPanel();
		getPriorityPanel();
		getBottomPanel();

		pack();
		Dimension dim = kit.getScreenSize();
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);
		setResizable(false);
	}

	private void getDateCreatedPanel() {
		if (dateCreatedLabel == null)
			dateCreatedLabel = new JLabel("Date created: ");
		if (dateCreatedTextArea == null) {
			SimpleDateFormat df = new SimpleDateFormat("dd.MM.YYY HH:mm:ss");
			dateCreatedTextArea = new JTextArea(
					df.format(task.getDateCreated()), 5, 10);
			dateCreatedTextArea.setEditable(false);
		}
		add(dateCreatedLabel);
		add(dateCreatedTextArea);
	}

	private void getPriorityPanel() {
		if (priorityLabel == null)
			priorityLabel = new JLabel("Priority: ");
		if (priorityTextArea == null) {
			priorityTextArea = new JTextArea(task.getPriority().toString(), 5,
					10);
			priorityTextArea.setEditable(editable);
		}
		add(priorityLabel);
		add(priorityTextArea);
	}

	private void getDescriptionPanel() {
		if (descriptionLabel == null)
			descriptionLabel = new JLabel("Description: ");
		if (descriptionTextArea == null) {
			descriptionTextArea = new JTextArea(task.getDescription(), 5, 10);
			descriptionTextArea.setLineWrap(true);
			descriptionTextArea.setEditable(editable);
		}
		if (descriptionScrollPane == null) {
			descriptionScrollPane = new JScrollPane(descriptionTextArea);
			descriptionScrollPane
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		}
		add(descriptionLabel);
		add(descriptionScrollPane);
	}

	private void getNamePanel() {
		if (nameLabel == null)
			nameLabel = new JLabel("Task name: ");
		if (nameTextArea == null) {
			nameTextArea = new JTextArea(task.getName(), 5, 10);
			nameTextArea.setLineWrap(true);
			nameTextArea.setEditable(editable);
		}
		if (nameScrollPane == null) {
			nameScrollPane = new JScrollPane(nameTextArea);
			nameScrollPane
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		}
		add(nameLabel);
		add(nameScrollPane);
	}

	private void getBottomPanel() {
		if (delete == null)
			delete = new JButton("Delete task");
		delete.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				AppCore.getInstance().removeTask(task);
				dispose();
				new ListTasksDialog();
			}
		});
		add(delete);
		if (cancel == null)
			cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				new ListTasksDialog();
			}
		});

		add(cancel);

		if (update == null) {
			update = new JButton("Update");
			update.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					new AddTaskDialog(task);
					dispose();
				}
			});
			add(update);
		}

		if (resolve == null) {
			if (!task.isResolved())
				resolve = new JButton("Resolve");
			else
				resolve = new JButton("Unesolve");
			resolve.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					task.setResolved(!task.isResolved());
					AppCore.getInstance().saveTaskList();
					new ListTasksDialog();
					dispose();
				}
			});
			add(resolve);
		}
	}

}
